/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ManagedConnectionListenerTester.cpp. Created on: 18.09.2009 13:29:37
 */

#include "ManagedConnectionListenerTester.hpp"

ManagedConnectionListenerTester::ManagedConnectionListenerTester() : m_poReceiveBuffer(NULL) {
	reset();
}

ManagedConnectionListenerTester::~ManagedConnectionListenerTester() {
	notifyWaiter();
	notifyWaiterPerHandler();
	reset();
}

void ManagedConnectionListenerTester::onConnected(IManagedConnection* poConnection) throw() {
	m_bConnected = true;
	notifyWaiter();
}

void ManagedConnectionListenerTester::onConnected(ConnectionHandler* poHandler) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	notifyWaiterPerHandler();
}

void ManagedConnectionListenerTester::onConnectionFailure(IManagedConnection* poManagedConnection, const std::string& sErrorMessage) throw() {
	m_bConnected = false;
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiter();
}

void ManagedConnectionListenerTester::onConnectionFailure(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiterPerHandler();
}

void ManagedConnectionListenerTester::onDisconnected(IManagedConnection* poConnection) throw() {
	m_bConnected = false;
	notifyWaiter();
}

void ManagedConnectionListenerTester::onDisconnected(ConnectionHandler* poHandler) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	notifyWaiterPerHandler();
}


void ManagedConnectionListenerTester::onReceiveError(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiterPerHandler();
}

void ManagedConnectionListenerTester::onWriteError(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiterPerHandler();
}

void ManagedConnectionListenerTester::onReceived(ConnectionHandler* poHandler, cpputils::io::ByteBuffer* poBuffer) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);

	if(m_poReceiveBuffer!=NULL)
		delete m_poReceiveBuffer;
	m_poReceiveBuffer = new ByteBuffer(poBuffer->getLen());
	m_poReceiveBuffer->append(*poBuffer);
	++this->m_nReceiveEventCounter;
	notifyWaiterPerHandler();
}

const unsigned ManagedConnectionListenerTester::getReceiveEventCounter(){
	return this->m_nReceiveEventCounter;
}

ByteBuffer* ManagedConnectionListenerTester::getLastReceivedMessage() {
	return m_poReceiveBuffer;
}


bool ManagedConnectionListenerTester::isConnected() {
	return m_bConnected;
}

std::string ManagedConnectionListenerTester::getLastErrorMessage() {
	return m_sLastErrorMessage;
}

void ManagedConnectionListenerTester::reset() {
	m_nReceiveEventCounter = 0;
	m_sLastErrorMessage = "";
	m_bConnected = false;
	foreach(ConnectionHandler* poHandler, m_aHandlers) {
		poHandler->release();
	}
	m_aHandlers.clear();
	if(m_poReceiveBuffer) {
		delete m_poReceiveBuffer;
		m_poReceiveBuffer = NULL;
	}
}

boost::recursive_mutex& ManagedConnectionListenerTester::getMonitor() {
	return m_mxMonitor;
}

boost::recursive_mutex& ManagedConnectionListenerTester::getMonitorPerHandler() {
	return m_mxMonitorPerHandler;
}

void ManagedConnectionListenerTester::waitForAnyEvent(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation /*= "Unknown location"*/) {
	boost::xtime oXt;
	boost::xtime_get(&oXt, boost::TIME_UTC);
	oXt.sec += 10; // 10 sec is max await time
	if(!m_evAwait.timed_wait(oLock,oXt)) {
		std::cout << "Timeout for waiting connection event! Location: " << sLocation <<std::endl;
		throw SystemException("Timeout for waiting connection event! Location: " + sLocation);
	}
}

void ManagedConnectionListenerTester::waitForAnyEventPerHandler(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation /*= "Unknown location"*/) {
	boost::xtime oXt;
	boost::xtime_get(&oXt, boost::TIME_UTC);
	oXt.sec += 10; // 10 sec is max await time
	if(!m_evAwaitPerHandler.timed_wait(oLock,oXt)) {
		std::cout << "Timeout for waiting connection event! Location: " << sLocation <<std::endl;
		throw SystemException("Timeout for waiting connection event! Location: " + sLocation);
	}
}


void ManagedConnectionListenerTester::notifyWaiter() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitor);
	m_evAwait.notify_one();
}

void ManagedConnectionListenerTester::notifyWaiterPerHandler() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	m_evAwaitPerHandler.notify_one();
}

ManagedConnectionListenerTester::Handlers& ManagedConnectionListenerTester::getLastHandlers() {
	return m_aHandlers;
}

void ManagedConnectionListenerTester::addLastHandler(ConnectionHandler *poHandler) {
	poHandler->addRef();
	m_aHandlers.push_back(poHandler);
}
